package com.changgou.search.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.entity.PageResult;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.pojo.Sku;
import com.changgou.search.pojo.SkuInfo;
import com.changgou.search.dao.ESManagerMapper;
import com.changgou.search.service.ESManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ESManagerServiceImpl implements ESManagerService {
    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;
    @Autowired
    private SkuFeign skuFeign;
    @Autowired
    private ESManagerMapper esManagerMapper;
    /**
     * 创建索引库
     */
    @Override
    public void creatIndexAndMapping() {
        //创建索引
        elasticsearchTemplate.createIndex(SkuInfo.class);
        //设置映射关系
        elasticsearchTemplate.putMapping(SkuInfo.class);
    }

    /**
     * 导入全部数据到索引库中
     */
    @Override
    public void importAll() {
        //查询所有的sku
        List<Sku> skuList = skuFeign.findSkuListBySpuId("all");
        if (skuList == null || skuList.size() <= 0){
            throw new RuntimeException("当前没有数据被查询到,无法导入索引库");
        }
        //将skuList转为JSAON
        String skuString = JSON.toJSONString(skuList);
        //将JSAON转为skuInfo
        List<SkuInfo> skuInfoList = JSON.parseArray(skuString, SkuInfo.class);
        for (SkuInfo skuInfo : skuInfoList) {
            Map searchMap = JSON.parseObject(skuInfo.getSpec(), Map.class);
            skuInfo.setSpecMap(searchMap);
        }
        //导入索引库
        esManagerMapper.saveAll(skuInfoList);
    }

    @Override
    public void importDataToESBySpuId(String spuId) {
        //根据spuId查询sku集合
        List<Sku> skuList = skuFeign.findSkuListBySpuId(spuId);
        if (skuList == null || skuList.size() <= 0){
            throw new RuntimeException("当前没有数据被查到,无法导入索引库");
        }
        //skuList转为JSON
        String skuString = JSON.toJSONString(skuList);
        //JSON转为skuInfo
        List<SkuInfo> skuInfoList = JSON.parseArray(skuString, SkuInfo.class);
        for (SkuInfo skuInfo : skuInfoList) {
            Map searchMap = JSON.parseObject(skuInfo.getSpec(), Map.class);
            skuInfo.setSpecMap(searchMap);
        }
        //导入索引库
        esManagerMapper.saveAll(skuInfoList);
    }

    @Override
    public void delDataBySpuId(String spuId) {
        //根据spuId查询sku集合
        List<Sku> skuList = skuFeign.findSkuListBySpuId(spuId);
        if (skuList == null || skuList.size() <= 0){
            throw new RuntimeException("当前没有数据被查到,无法删除索引库");
        }
        for (Sku sku : skuList) {
            esManagerMapper.deleteById(Long.valueOf(sku.getId()));
        }
    }

    /**
     * 分页导入数据
     */
    @Override
    public void importData() {
        // 远程获取sku数据列表
        PageResult<Sku> pageResult = skuFeign.findSkuPageBySpuId("all", 1);

        if (pageResult == null || pageResult.getTotal() == 0 || pageResult.getPages() == 0) {
            throw new RuntimeException("当前没有数据被查到,无法导入索引库");
        }
        // 获取总页数
        Integer pages = pageResult.getPages();
        for (int i = 1; i <= pages; i++) {
            List<Sku> skuList = skuFeign.findSkuPageBySpuId("all", i).getRows();

            //skulist转换为json   list<Sku> --》 JSON  --》 List<skuInfo>
            String jsonSkuList = JSON.toJSONString(skuList);
            //将json转换为skuinfo
            List<SkuInfo> skuInfoList = JSON.parseArray(jsonSkuList, SkuInfo.class);

            for (SkuInfo skuInfo : skuInfoList) {
                //将规格信息转换为map  {'颜色': '红色', '版本': '8GB+128GB'}  =》map
                Map specMap = JSON.parseObject(skuInfo.getSpec(), Map.class);
                skuInfo.setSpecMap(specMap);
            }

            //导入索引库
            esManagerMapper.saveAll(skuInfoList);
            System.out.println("导入 第"+i+"页 SKU数据列表成功");
        }
    }
}
